package com.xbs.service.enterprise;

import java.util.ArrayList;
import java.util.List;

import com.xbs.dao.enterprise.EnterpriseIndex;
import com.xbs.entity.CarModelEntity;
import com.xbs.entity.EnterpriseModelEntity;
import com.xbs.entity.EnterpriseMotorcadeEntity;
import com.xbs.entity.EnterpriseMotorcadeSumEntity;
import com.xbs.entity.EnterpriseOrderDetailEntity;
import com.xbs.entity.EnterpriseOrderEntity;
import com.xbs.utils.EnterpriseUtils;
import com.xbs.utils.EntityPrimaryKey;

public class EnterpriseIndexService {
	private EnterpriseIndex dao=new EnterpriseIndex();
	/**
	 * 获取车型表
	 */
	public List<CarModelEntity> getCarmodel(String epid){
		return dao.getCarModel(epid);
	}
	/**
	 * 返回车队信息
	 */
	public List<EnterpriseMotorcadeSumEntity> getMotorcade(){
		//获取车车队的列表
		List<EnterpriseMotorcadeSumEntity> ist=new ArrayList<>();
		List<EnterpriseMotorcadeEntity> ist1=dao.getMotorcade();
		
		for (int i = 0; i < ist1.size(); i++) {
			//所属车队编号
			int motorcadeid=ist1.get(i).getBelongcarmotorcade();
			
			EnterpriseMotorcadeSumEntity ep=new EnterpriseMotorcadeSumEntity();
			double sum=0;
			double sum1=0;
			for(int j=i+1;j<ist1.size();j++){
				
				if(motorcadeid==ist1.get(j).getBelongcarmotorcade()){
					sum+=ist1.get(j).getCar_money();
					sum1+=ist1.get(j).getCar_model_rent_cash();
					ep.getMotos().add(ist1.get(j));
					ist1.remove(j);
				}
			}
			ep.setCar_money(sum);
			ep.setPlan_car_money(sum1);
			ep.getMotos().add(ist1.get(i));
			ep.setMotorcade_end_time(ist1.get(i).getMotorcade_end_time());
			ist.add(ep);
			
		}
		
		return ist;
	}
	/**
	 * 保存企业订单
	 */
	public void saveEnterpriseOrder(EnterpriseOrderEntity eoe){
		dao.saveEnterpriseOrder(eoe);
	}
	
	/**
	 * 将多个企业订单车型对象插入到企业订单车型表中
	 */
	public void saveEnterpriseModels(List<EnterpriseModelEntity> ist){
		for(EnterpriseModelEntity e:ist){
			dao.saveEnterpriseModel(e);
		}
	}
	/**
	 * 根据车型编号获取企业租金
	 */
	public Double getEnterpriseRent(String id){
		return dao.getRentCashByCarModelId(id);
	}
	/**
	 * 获取待确认车型订单实体
	 */
	public List<EnterpriseOrderDetailEntity> getCarModelByIds(String[] ids,String[] carnums){
		List<EnterpriseOrderDetailEntity> carmodels=dao.getCarModelByIds(ids);
		/**
		 * 将CarModelEntity的数据放入到EnterpriseOrderDetailEntity中
		 */
		EnterpriseUtils util=new EnterpriseUtils();
		EntityPrimaryKey<EnterpriseOrderDetailEntity,String> primary=new EntityPrimaryKey<EnterpriseOrderDetailEntity, String>(){
			@Override
			public boolean equals(String key, EnterpriseOrderDetailEntity entity) {
				return key.equals(entity.getCarid());
			}
			@Override
			public void putInEntity(EnterpriseOrderDetailEntity entity, String value) {
				entity.setNum(Integer.parseInt(value));
			}
		};
		util.mergerEntityandArray(ids, carmodels, carnums, primary);
		
		
		return carmodels;
	}
	/**
	 * 根据车队所属编号获取车信息
	 */
	public List<EnterpriseOrderDetailEntity> getCarmotoById(String id){
		return dao.getMotoCarById(id);
	}
}
